//
//  AFFRichTextURL.m
//  AnyfishApp
//
//  Created by Bob Lee on 14-4-18.
//  Copyright (c) 2014年 Anyfish. All rights reserved.
//

#import "AFFRichTextURL.h"
#import "AFFRichTextExpression.h"

@implementation AFFRichTextURL

- (id)init
{
    self = [super init];
    if (self) {
        self.textType = ERichTextType_URL;
        self.isResponseTouch = YES;
//        self.textColor = kSetting.color_ff9800;
    }
    return self;
}

//-- 替换基础文本
- (void)replaceTextWithAttributedString:(NSMutableAttributedString*) attributedString
{
    if(self.range.length>0){
        if(self.range.length>attributedString.length)
            return;
        
        if(self.textColor)
            [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)self.textColor.CGColor range:self.range];
        
        if(self.originalFont)
            [attributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)self.originalFont range:self.range];
        
        [super replaceTextWithAttributedString:attributedString];
    }
}

//-- 绘制内容
- (BOOL)drawRunWithRect:(CGRect)rect
{
    return NO;
}

/**
 * @function
 * 说明 特殊处理，评论，点赞，回复等提前解析名称中得特殊表情
 仅支持插入UrlRun
 * @param arr runs只能包含表情和特殊文本
 * @param urlRun
 * @param
 
 * @return
 */
+ (void)inserRunUrl:(NSMutableArray*)arr urlRun:(AFFRichTextURL*)urlRun{
    if(!arr)
        return;
    
    if(arr.count<=0){
        [arr addObject:urlRun];
        return;
    }
    
    for(int i=0;i<arr.count;i++){
        AFFRichTextBase *run = [arr objectWithIndex:i];
        if([run isKindOfClass:[AFFRichTextExpression class]]){
            [arr insertObject:urlRun atIndex:i];
            return;
        }
    }
    
    [arr addObject:urlRun];
}

+ (void)analyzeTextObjEx:(AFFDataText*)msgText runsArray:(NSMutableArray **)urlArray{
    if(msgText && msgText.array.count>0){
        NSString *finalText = @"";
        AFFRichTextURL *urlRun;
        BOOL hasHandle = NO;
        
        for(AFFDataPartion *partion in msgText.array){
            // 如果这个partion包含特殊表情需要先解析，并且更新原始显示文本
            NSString *original = partion.textAnalyze?partion.textAnalyze:@"";
            NSRange range = [original rangeOfString:@"[" options:NSBackwardsSearch];
            if(range.length>0){
                partion.textShow = original = [AFFRichTextExpression analyzeText:original runsArray:urlArray location:finalText.length];
                if(![NSString isNilOrEmpty:partion.textDecode]){
                    partion.textDecode = original;
                }
            }
            
            // 解析特殊名称
            if(partion.code>0||partion.code2>0){
                // 特殊处理评论鱼圈时的发起者，即回复谁，一定是第一个，isHander仅用于鱼圈回复，如果以后另有它用要做处理
                if(urlArray){
                    urlRun = [[AFFRichTextURL alloc] init];
                    urlRun.isHandler = !hasHandle;
                    urlRun.range = NSMakeRange(finalText.length, original.length);
                    urlRun.code = partion.code;
                    urlRun.originalText = [NSString stringWithString:original];
                    if(partion.color){
                        urlRun.textColor = partion.color;
                    }
                    
                    if(partion.fontSize>0){
                        urlRun.originalFont = [UIFont systemFontOfSize:partion.fontSize];
                    }
                    
                    hasHandle = YES;
                    [AFFRichTextURL inserRunUrl:*urlArray urlRun:urlRun];
                }
            }else{
                if(partion.color || partion.fontSize>0){
                    if(urlArray){
                        urlRun = [[AFFRichTextURL alloc] init];
                        urlRun.range = NSMakeRange(finalText.length, original.length);
                        urlRun.originalText = [NSString stringWithString:original];
                        if(partion.color){
                            urlRun.textColor = partion.color;
                        }
                        
                        if(partion.fontSize>0){
                            urlRun.originalFont = [UIFont systemFontOfSize:partion.fontSize];
                        }
                        
                        [AFFRichTextURL inserRunUrl:*urlArray urlRun:urlRun];
                    }
                }
            }
            
            finalText = [finalText stringByAppendingFormat:@"%@",original];
        }
    }
}

+ (NSString*)analyzeText:(AFFDataPartion*)partion runsArray:(NSMutableArray **)urlArray{
    
    NSString *finalText = @"";
    AFFRichTextURL *urlRun;
    BOOL hasHandle = NO;
    
    // 如果这个partion包含特殊表情需要先解析，并且更新原始显示文本
    NSString *original = partion.textAnalyze?partion.textAnalyze:@"";
    NSString *fixed = [[NSString alloc] initWithString:partion.textAnalyze];
    NSRange range = [original rangeOfString:@"[" options:NSBackwardsSearch];
    if(range.length>0){
        partion.textShow = original = [AFFRichTextExpression analyzeText:original runsArray:urlArray location:finalText.length];
        if(![NSString isNilOrEmpty:partion.textDecode]){
            partion.textDecode = original;
        }
    }
    
    // 解析特殊名称
    if(partion.code>0||partion.code2>0){
        // 特殊处理评论鱼圈时的发起者，即回复谁，一定是第一个，isHander仅用于鱼圈回复，如果以后另有它用要做处理
        if(urlArray){
            urlRun = [[AFFRichTextURL alloc] init];
            urlRun.isHandler = !hasHandle;
            urlRun.range = NSMakeRange(finalText.length, original.length);
            urlRun.code = partion.code;
            urlRun.originalText = [NSString stringWithString:original];
            if(partion.color){
                urlRun.textColor = partion.color;
            }
            
            if(partion.fontSize>0){
                urlRun.originalFont = [UIFont systemFontOfSize:partion.fontSize];
            }
            
            hasHandle = YES;
            [AFFRichTextURL inserRunUrl:*urlArray urlRun:urlRun];
        }
    }else{
        if(partion.color || partion.fontSize>0){
            if(urlArray){
                urlRun = [[AFFRichTextURL alloc] init];
                urlRun.range = NSMakeRange(finalText.length, original.length);
                urlRun.originalText = [NSString stringWithString:original];
                if(partion.color){
                    urlRun.textColor = partion.color;
                }
                
                if(partion.fontSize>0){
                    urlRun.originalFont = [UIFont systemFontOfSize:partion.fontSize];
                }
                
                [AFFRichTextURL inserRunUrl:*urlArray urlRun:urlRun];
            }
        }
    }
    
    return original;
    
}

@end
